package com.etc.zmall.dao.impl;

import java.util.List;

import com.etc.util.DBUtil;
import com.etc.util.PageData;
import com.etc.zmall.dao.EvaluateDao;
import com.etc.zmall.entity.Evaluate;
import com.etc.zmall.entity.ShopEvaluate;

/**
 * 评价DAO实现类
 * 
 * @author Administrator
 *
 */
public class EvaluateDaoImpl implements EvaluateDao {
	/**
	 * 添加评价
	 */
	@Override
	public boolean addEvaluate(Evaluate evaluate) {
		if (evaluate != null) {
			String sql = "insert into tbl_evaluate values (null,?,?,?)";
			return DBUtil.exUpdate(sql, evaluate.getUserid(), evaluate.getGoodsid(), evaluate.getEvaluate());
		}
		return false;
	}

	/**
	 * 删除评价
	 */
	@Override
	public boolean delEvaluate(int evaluateid) {
		String sql = "delete from tbl_evaluate where evaluateid = ? ";
		return DBUtil.exUpdate(sql, evaluateid);
	}

	/**
	 * 查询某一商品的所有评价
	 */
	@Override
	public List<Evaluate> queryEvaluate(int goodsid) {

		String sql = "select * from tbl_evaluate where goodsid = ? ";
		List<Evaluate> list = (List<Evaluate>) DBUtil.exQuery(sql, Evaluate.class, goodsid);
		// 判断是否找到该商品的所有评价
		if (list.size() == 0) {
			return null;
		}
		return list;
	}

	/**
	 * 分页查询
	 */
	@Override
	public PageData<Evaluate> queryPageEvaluate(int page, int pageSize, int goodsid) {
		// TODO Auto-generated method stub
		String sql = "select * from tbl_evaluate where goodsid = ? ";

		return DBUtil.exQueryByPage(sql, Evaluate.class, page, pageSize, goodsid);
	}

	/**
	 * 
	 */
	@Override
	public PageData<ShopEvaluate> queryPageshopEvaluate(int page, int pageSize, int shopid) {
		// TODO Auto-generated method stub
		String sql = "select shopgoods.goodsname,tbl_evaluate.userid,tbl_evaluate.evaluate from tbl_evaluate,"
				+ "(select * from tbl_goods where tbl_goods.shopid=?) as shopgoods "
				+ "where tbl_evaluate.goodsid=shopgoods.goodsid";
		return DBUtil.exQueryByPage(sql, ShopEvaluate.class, page, pageSize, shopid);
	}

}
